export function checkFileType(file: File, accepts: string[]) {
    const newTypes = accepts.join('|')
    // const reg = /\.(jpg|jpeg|png|gif|txt|doc|docx|xls|xlsx|xml)$/i;
    const reg = new RegExp(`${String.raw`\.(` + newTypes})$`, 'i')

    return reg.test(file.name)
}

export function checkImgType(file: File) {
    return isImgTypeByName(file.name)
}

export function isImgTypeByName(name: string) {
    return /\.(?:jpg|jpeg|png|gif|webp|mp4)$/i.test(name)
}

export function getBase64WithFile(file: File) {
    return new Promise<{
        file: File
        result: string
    }>((resolve, reject) => {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.addEventListener('load', () => resolve({ result: reader.result as string, file }))
        // eslint-disable-next-line unicorn/prefer-add-event-listener
        reader.onerror = (error) => reject(error)
    })
}
